<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="home-title">
          <img src="../assets/logo.png">
          <p>美多优选商城后台管理系统</p>
        </div>
          <el-button type="info" round @click="signOut">安全退出商城管理系统</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
           <el-button type="info" :icon="btnIcon" @click="handleCollapse"></el-button>
           <el-menu router
             background-color="#333744"
             text-color="#fff"
             unique-opened
             active-text-color="#F78989"
             :collapse="isCollapse"
             :default-active="indexPath"
             :collapse-transition="false">
             <el-submenu v-for="items in menuList" :key="items.id" :index="items.id+''">
               <template slot="title">
                 <i :class="menuIcon[items.path]" class="menuicon"></i>
                 <span>{{items.authName}}</span>
               </template>
                 <el-menu-item v-for="item in items.children" @click="handleActive(item.path)" :key="item.id" :index="'/'+item.path">
                   <i class="el-icon-menu"></i>
                   <span>{{item.authName}}</span>
                 </el-menu-item>
             </el-submenu>
           </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Home',
  created () {
    this.getMenuList()
    this.activePath()
  },
  data () {
    return {
      menuList: [],
      menuIcon: {
        users: 'iconfont icon-users',
        rights: 'iconfont icon-tijikongjian',
        goods: 'iconfont icon-shangpin',
        orders: 'iconfont icon-danju',
        reports: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      btnIcon: 'el-icon-d-arrow-left',
      indexPath: ''
    }
  },
  methods: {
    async signOut () {
      const username = sessionStorage.getItem('username')
      const confirmRes = await this.$confirm(` ${username}：确定要狠心离开吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes !== 'confirm') {
        this.$notify({
          title: `${username}：Thanks~`,
          message: '多一分停留，少一分想念...',
          type: 'info',
          offset: 100
        })
        return
      }
      this.$notify({
        title: `${username}：Good Bye~`,
        message: '有缘千里来相会，相逢何必曾相识...',
        offset: 100,
        type: 'warning'
      })
      sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      const addItemIndex = res.data.findIndex(item => item.authName === '数据统计')
      const newObj = { 
        authName: '概况总览',
        children: [],
        
      }
      res.data[addItemIndex]
      this.menuList = res.data
    },
    activePath () {
      this.indexPath = sessionStorage.getItem('indexPath')
    },
    handleCollapse () {
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {
        this.btnIcon = 'el-icon-d-arrow-right'
      } else {
        this.btnIcon = 'el-icon-d-arrow-left'
      }
    },
    handleActive (index) {
      const path = '/' + index
      sessionStorage.setItem('indexPath', path)
      this.indexPath = path
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373D41;
  padding-left: 0;
  .home-title {
    display: flex;
    align-items: center;
    p {
      font-size: 24px;
      margin-left: 12px;
      color: #DFEAF9;
    }
    img {
      margin-left: 8px;
      width: 56px;
    }
  }
}
.el-aside {
  .el-button {
    border-radius: 0;
    padding: 0;
    width: 100%;
    height: 24px;
  }
  background-color: #333744;
  .el-menu {
    border-right: 0;
    .el-submenu {
      .menuicon {
        font-size: 24px;
        margin-right: 10px;
    }
  }
  }
}
.el-main {
  background-color: #EAEDF1;
}
.home-container {
  height: 100%;
  .el-container {
    height: 100%;
  }
}

</style>
